package com.feel.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.feel.exception.SteduException;
import com.feel.mapper.UserMapper;
import com.feel.pojo.Appointment;
import com.feel.pojo.User;
import com.feel.service.AppointmentService;
import com.feel.service.UserService;
import com.feel.utils.JwtUtil;
import com.feel.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 刘永钦
 * @Date: 2024-09-03-9:25
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AppointmentService appointmentService;

    @Override
    public PageInfo<User> getAll(Integer currentPage, String role, String name) throws SteduException {
        Integer id = ThreadLocalUtil.get();
        User user = userMapper.getById(id);
        if (user.getRole().equals("user")) {
            throw new SteduException("权限错误");
        }
        PageHelper.startPage(currentPage, 5);
        // todo 目前没有做按照部门进行分页的业务
        List<User> list = userMapper.getAll(role, name, null);
        PageInfo<User> userPageInfo = new PageInfo<>(list);
        return userPageInfo;
    }

    @Override
    public String login(User user) throws SteduException {
        User localUser = userMapper.login(user.getUsername());
        if (localUser == null || !localUser.getPassword().equals(SecureUtil.md5(user.getPassword()))) {
            throw new SteduException("账号或密码错误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("username", localUser.getUsername());
        map.put("userId", localUser.getUserId());
        // 生成token
        String token = JwtUtil.generateJwt(map);
        return token;
    }

    @Override
    public Boolean updateUser(User user) throws SteduException {
        User login = userMapper.login(user.getUsername());
        if (login != null && !login.getUserId().equals(user.getUserId())) {
            throw new SteduException("账户重复请重试");
        }
        Integer i = userMapper.updateUser(user);
        return i > 0;
    }

    @Override
    public Boolean addUser(User user) throws SteduException {
        User login = userMapper.login(user.getUsername());
        if (login != null) {
            throw new SteduException("账户重复请重试");
        }
        Integer i = userMapper.addUser(user);
        return i > 0;
    }

    @Override
    public Boolean delUser(Integer userId) throws SteduException {
        // todo 需要进行判断，如果是职员，需要判断名下是否有订单，
        Integer id = ThreadLocalUtil.get();
        User user = userMapper.getById(id);
        if (!user.getRole().equals("root")) {
            throw new SteduException("权限错误");
        }
        User delUser = userMapper.getById(userId);
        if (delUser.getRole().equals("emp")) {
            //    判断员工是否存在订单
            Appointment appointment = new Appointment();
            appointment.setEmpId(userId);
            PageInfo<Appointment> info = appointmentService.getAll(1, appointment);
            if (info != null && info.getList().size() > 0) {
                throw new SteduException("禁止删除该员工存在未完成订单");
            }
        }
        Integer i = userMapper.delUser(userId);
        return i > 0;
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public List<User> getList(Integer did) throws SteduException {
        List<User> list = new ArrayList<>();
        if (did != null) {
            if (did > 0) {
                list = userMapper.getAll("emp", null, did);
            } else {
                list = userMapper.getAll("user", null, null);
            }

        } else {
            Integer id = ThreadLocalUtil.get();
            User user = userMapper.getById(id);
            if (user.getRole().equals("user")) {
                throw new SteduException("权限错误");
            }
            list = userMapper.getAll("emp", null, user.getDepartmentId());
        }

        return list;
    }

    @Override
    public Integer getUserNumber(String role) {
        Integer userNumber = userMapper.getUserNumber(role);
        return userNumber;
    }

    @Override
    public Map<String, Object> getSexRatio() {
        List<User> list = userMapper.getAll("user", null, null);
        long n = list.stream()
                .filter(item -> "男".equals(item.getGender()))
                .count();
        long count = list.stream()
                .filter(item -> "女".equals(item.getGender()))
                .count();
        Map<String, Object> map = new HashMap<>();
        map.put("man", n);
        map.put("woman", count);
        return map;
    }
}
